<template>
	<view >
		<com-payment-modal ref="ssModal" :H5Bottom="_H5Bottom" :mode="_parentMode" :position="_parentPosition" @change="changeModal">
			<view class="digital-keyboard-modal" :class="'digital-keyboard-modal-' + _mode">
				<view class="dk-close" v-if="_mode !== 1 && _mode !== 4" @click="close()">
					<icon type="clear" color="#333" :size="u50"></icon>
				</view>
				<swiper class="dk-swiper" :current="current">
					<swiper-item @touchmove.prevent.stop>
						<view class="dk-title">{{_titleObj.title}}</view>
						<view class="dk-subtitle">{{_titleObj.subTitle}}</view>
						<view class="pwd-box clearfix" @click="getKeyboard">
							<view class="pwd-text" v-for="(item,index) in _digits" :key="index" :data-index="index" :class="{active:(activeInput == index)}">{{payPassWord[index]}}</view>
						</view>
					</swiper-item>
					<!-- 设置新安全密码 -->
					<block v-if="_mode === 2 || _mode === 5">
						<swiper-item @touchmove.prevent.stop>
							<view class="dk-title">{{_titleObj.title}}</view>
							<view class="dk-subtitle">{{_titleObj.twoSubTitle}}</view>
							<view class="pwd-box clearfix" @click="getKeyboard">
								<view class="pwd-text" v-for="(item,index) in _digits" :key="index" :data-index="index" :class="{active:(activeInput==index)}">{{payPassWord[index]}}</view>
							</view>
						</swiper-item>
					</block>
					<!-- 修改安全密码 -->
					<block v-else-if="_mode === 3">
						<swiper-item @touchmove.prevent.stop>
							<view class="dk-title">{{_titleObj.twoTitle}}</view>
							<view class="dk-subtitle">{{_titleObj.twoSubTitle}}</view>
							<view class="pwd-box clearfix" @click="getKeyboard">
								<view class="pwd-text" v-for="(item,index) in _digits" :key="index" :data-index="index" :class="{active:(activeInput==index)}">{{payPassWord[index]}}</view>
							</view>
						</swiper-item>
						<swiper-item @touchmove.prevent.stop>
							<view class="dk-title">{{_titleObj.threeTitle}}</view>
							<view class="dk-subtitle">{{_titleObj.threeSubTitle}}</view>
							<view class="pwd-box clearfix" @click="getKeyboard">
								<view class="pwd-text" v-for="(item,index) in _digits" :key="index" :data-index="index" :class="{active:(activeInput==index)}">{{payPassWord[index]}}</view>
							</view>
						</swiper-item>
					</block>
				</swiper>
				<block v-if="_mode !== 1 && _mode !== 4">
					<view class="pwd-tips">安全密码为6位数字，用于提现、设置密保、添加银行卡等操作，可保障资金安全</view>
					<view class="pwd-tips pwd-tips-errot">{{msgText}}</view>
				</block>
				<view class="pwd-forget">
					<text v-if="_forget && _mode === 1" @click="forgetPwd">忘记安全密码</text>
				</view>
				<view class="digital-keyboard" :class="{'digital-keyboard-show' : dkFlag || _mode === 1 || _mode === 4}">
					<view class="dk-down" v-if="_mode !== 1 && _mode !== 4">
						<block v-if="showPrevText">
							<view class="dk-submit dk-return" @click="changeSwiper(-1)">上一步</view>
						</block>
						<svg class="icon icon-down" aria-hidden="true" @click="getKeyboard">
						  <use xlink:href="#icon-down"></use>
						</svg>
						<block v-if="trigger === 'auto'">
							<view class="dk-submit" @click="submit(true)">完成</view>
						</block>
					</view>
					<view class="digital-key-box clearfix">
						<view class="num" v-for="item in digitalList" :key="item" :class="{'no-num':(item === '' || item === '-1')}"
						 @click="getKeyNumber(item)">{{item}}</view>
					</view>
				</view>
			</view>
		</com-payment-modal>
	</view>
</template>

<script>
	//依赖的弹窗模块
	// import sanshuiModal from '@/components/sanshui-modal'
	export default {
		// components: {
		// 	sanshuiModal
		// },
		data() {
			return {
				activeInput: 0,//当前输入的下标
				digitalList: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '', '0', '-1'],//键盘
				paymentPwd: '',//安全密码内容
				dkFlag: true,//安全密码键盘的显示与隐藏
				current: 0,
				paymentPwds: [],//上一次安全密码内容,历史安全密码,
				msgText: '',
				u50: uni.upx2px(50)
			};
		},
		props: {
			// 密码输入的默认值
			value: {
				type: String,
				default: ''
			},
			// 密码输入是几位数（最多7位）
			digits: {
				type: [Number, String],
				default: 6
			},
			// 是否显示忘记密码（忘记密码跳转地址自己进去里面填）
			forget: {
				type: Boolean,
				default: false
			},
			/*
			*	可选值说明（这一个顺序最好不要改，跟当前步骤步进行有关系，自动提交要用到mode跟current的判断  是否要自动提交）
			*	1 安全密码校验（用于校验安全）
			*	2 设置安全密码（2步  输入=》确认输入）
			*	3 修改安全密码（3步 原安全密码=》输入=》确认输入）
			*	4 找回登录密码(同mode = 1一样，就怕后端校验接口不一样而额外添加的一个mode)
			*	5 重置安全密码
			*/
			mode: {
				type: [Number, String],
				default: 1
			},
			/*
			*	可选值说明
			*	hand 手动
			*	auto 自动
			*/
			trigger: {
				type: String,
				default: 'auto'
			},
			/*
			* H5置底
			*/
			H5Bottom: {
				type: Boolean,
				default: false
			}
		},
		computed: {
			_H5Bottom() {
				return this.H5Bottom;
			},
			payPassWord () {
				var payPassWord = this.paymentPwd.split('') || [];
				payPassWord.fill('*');
				return payPassWord;
			},
			_forget () {
				return String(this.forget) === 'false' ? false : true;
			},
			_digits () {
				return isNaN(this.digits) ? 6 : parseInt(this.digits);
			},
			_mode () {
				if(isNaN(this.mode) || !this.mode){
					return 1;
				}
				return +this.mode
			},
			_titleObj () {
				let arr = [
					{
						title: '请输入支付密码',
						subTitle: '为了保证您的账户安全，请输入您的支付密码'
					},
					{
						title: '设置安全密码',
						subTitle: '请设置安全密码',
						twoSubTitle: '请再次输入安全密码以确认'
					},
					{
						title: '修改安全密码',
						twoTitle: '设置安全密码',
						threeTitle: '设置安全密码',
						subTitle: '请输入安全密码 以验证身份',
						twoSubTitle: '请设置新的安全密码',
						threeSubTitle: '请再次输入安全密码以确认',
					},
					{
						title: '输入安全密码',
						subTitle: '请输入安全密码'
					},
					{
						title: '重置安全密码',
						subTitle: '请输入安全密码',
						twoSubTitle: '请再次输入安全密码以确认'
					}
				]
				return arr[this._mode-1];
			},
			_parentMode () {
				return this._mode === 1 || this._mode === 4 ? 'full' : 'cover';
			},
			_parentPosition () {
				return this._mode === 1 || this._mode === 4 ? 'bottom' : 'middle';
			},
			showPrevText () {
				return this.current >= (this._mode - 1) || (this._mode === 5 && this.current > 0) ? true : false;
			}
		},
		methods: {
			// 模态框显示或者关闭的回调
			changeModal (e) {// 返回是一个Boolean值
				if(e){
					this.current = 0;
					this.dkFlag = true;//默认拉起键盘
				}else{
					this.dkFlag = false;//默认收起键盘
					this.clearAll();
				}
			},
			/*
				pro可选址
				show	显示
				hide	隐藏
				toggle	动态判断
			*/
			modalFun(pro){
				this.$refs.ssModal[pro]();
			},
			clear () {
				this.paymentPwd = '';
				this.activeInput = 0;
				this.paymentPwds.splice(this.current, 1);
			},
			clearAll () {
				this.paymentPwd = '';
				this.activeInput = 0;
				this.current = 0;
				this.paymentPwds = [];
			},
			showError (msg) {
				if(!msg) return this.msgText = '';
				if(this._mode !== 1 && this._mode !== 4){
					this.msgText = msg;
				}else{
					uni.showToast({
						title: msg
					})
				}
			},
			forgetPwd () {
				console.log('这里是忘记密码跳转逻辑');
			},
			// 弹窗取消按钮（点击阴影处会自动关闭弹窗）
			cancel () {
				this.$emit('cancel');
			},
			close () {
				let tips = '';
				if(this._mode === 2){
					tips = '您是否要放弃设置安全密码';
				}else if(this._mode === 5){
					tips = '您是否要放弃重置安全密码';
				}else{
					tips = '您是否要放弃修改安全密码';
				}
				uni.showModal({
					title: '关闭提示',
					content: tips,
					confirmText: '我再想想',
					cancelText: '关闭',
					success: (res) => {
						if(!res.confirm){
							this.modalFun('hide');
						}
					}
				})
			},
			// 输入密码到指定位数（就是等于digits的值）的时候自动触发的函数
			submit (clickFlag){//clickFlag主动点击
				if(this.trigger !== 'auto' && !clickFlag) return;
				if(this.paymentPwd.length !== this._digits){
					this.showError('请输入' + this._digits + '位安全密码');
				}else{
					this.showError('');
					if(this._mode === 5 || this._mode === 4 || this.current >= (this._mode - 1)){
						if(this._mode === 5 && this.current === 0){//下一步，再次输入
							return this.changeSwiper(1);
						}
						if(this.current > 0){//需要校验2次安全密码的是否相等
							if(this.paymentPwd !== this.paymentPwds[this.current - 1]){
								this.showError('两次安全密码输入不一致');
								this.paymentPwd = '';
								this.paymentPwds[this.current] = '';
								return ;
							}
							this.showError('');
							if(this._mode === 2){//设置安全密码
								uni.showLoading({
									title: '正在设置密码'
								});
								// 如果不把逻辑卸载这里面，直接把setTimeout删除即可
								setTimeout(() => {//模拟请求，把这个换成ajax请求哈，或者emit出去 外面去请求，但是建议都集成在这边 省的写一堆重置代码
									uni.hideLoading();
									let response = {//模拟返回
										code: 1,
										value: this.paymentPwd
									}
									if(response.code === 1){
										this.$emit('submit', {//这里是传出去给调用者的，参数任意下，只要能区分就行
											type: 'reset',
											value: this.paymentPwd
										});
										this.modalFun('hide');
									}
								}, 1000)
							}else{//修改安全密码
								uni.showLoading({
									title: '正在重置密码'
								});
								// 如果不把逻辑卸载这里面，直接把setTimeout删除即可
								setTimeout(() => {//模拟请求，把这个换成ajax请求哈，或者emit出去 外面去请求，但是建议都集成在这边 省的写一堆重置代码
									uni.hideLoading();
									let response = {//模拟返回
										code: 1,
										msg: '重置新安全密码成功'
									}
									if(response.code === 1){
										this.$emit('submit', {
											type: 'modify',
											value: this.paymentPwd
										});
										this.modalFun('hide');
									}
								}, 1000)
							}
							return ;
						}
						this.checkSafePwd('check');
					}else{
						if(this._mode === 3 && this.current === 0){//校验安全密码正确性
							this.checkSafePwd('verify');
							return ;
						}
						this.changeSwiper(1);
					}
				}
			},
			checkSafePwd(type) {
				this.$emit('checkSafePwd',{passwork:this.paymentPwd});
				// uni.showLoading({
				// 	title: '校验安全密码中'
				// });
				// setTimeout(() => {//模拟请求，把这个换成ajax请求哈，或者emit出去 外面去请求，但是建议都集成在这边 省的写一堆重置代码
				// 	uni.hideLoading();
				// 	let response = {//模拟返回
				// 		code: 1,
				// 		msg: '密码正确'
				// 	}
				// 	if(response.code === 1){
				// 		if(type === 'check'){
				// 			this.$emit('submit', {//设置或者重置新安全密码
				// 				type: 'check',
				// 				value: this.paymentPwd
				// 			});
				// 			this.modalFun('hide');
				// 		}else{
				// 			this.changeSwiper(1);//执行下一步
				// 		}
				// 	}else{
				// 		this.clear();
				// 		if(type !== 'check'){
				// 			this.showError(response.msg);
				// 		}
				// 	}
				// }, 1000)
			},
			changeSwiper (num) {
				if(isNaN(num)){
					num = 1;
				}
				this.current += num;
				this.paymentPwd = this.paymentPwds[this.current] || '';
				this.activeInput = this.paymentPwd.length - 1;
			},
			getKeyNumber (val) {
				let paymentPwdArr = this.paymentPwd.split('');
				if (val === '' || (val != -1 && paymentPwdArr.length === this._digits)) { //空或者已经达到最大值
					if(paymentPwdArr.length === this._digits && this.trigger === 'auto'){
						this.submit();
						return ;
					}
					return false;
				} else if (val != -1) { //数字输入
					paymentPwdArr.splice(this.activeInput, 0, val);
					this.activeInput++;
					this.paymentPwd = paymentPwdArr.join('');
					this.paymentPwds[this.current] = this.paymentPwd;
					if(paymentPwdArr.length === this._digits && this.trigger === 'auto'){
						this.submit();
						return ;
					}
				} else { //删除
					if (this.activeInput >= 0) {
						this.activeInput !== 0 && this.activeInput--;
						paymentPwdArr.splice(this.activeInput - 1, 1);
						this.paymentPwd = paymentPwdArr.join('');
						this.paymentPwds[this.current] = this.paymentPwd;
					}
				}
			},
			getKeyboard (e) {
				let index = e && e.target.dataset.index;
				if (index === undefined) {
					this.activeInput = -1;
					this.dkFlag = false
					return ;
				}
				var _length = this.paymentPwd.length;
				this.activeInput = index <= _length ? index : _length;
				this.dkFlag = true;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.digital-keyboard-modal{
		height: 100%;
		background-color: #fff;
		&.digital-keyboard-modal-1, &.digital-keyboard-modal-4 {
			height: auto;
			border-radius: 20px 20px 0px 0px;
			// background: #fff url() center top / 100% auto no-repeat;
			padding-padding: 0;
			padding-padding: constant(safe-area-inset-top);
			padding-padding: env(safe-area-inset-top);
		}
		&.digital-keyboard-modal-2, &.digital-keyboard-modal-5, &.digital-keyboard-modal-3{
			padding-top: 180rpx;
			.dk-title{
				color: #000;
			}
			.digital-keyboard{
				width: 100%;
				position: fixed;
				bottom: 0;
				left: 0;
				position: fixed;
				left: 0;
				bottom: 0;
			}
		}

		.dk-title {
			font-size: 30rpx;
			font-weight: 400;
			color: #1F1F1F;
			line-height: 110rpx;
			text-align: center;
			border-bottom: 2rpx solid #E0E0E0;
		}
		
		.dk-swiper{
			// height: 260rpx;
			height: 300rpx;
		}
		
		.dk-submit{
			position: absolute;
			right: 0;
			top: 0;
			padding: 0 20rpx;
			color: #38f;
			font-size: 28rpx;
			line-height: 70rpx;
		}
		
		.dk-return{
			right: auto;
			left: 0;
		}

		
		.dk-close{
			position: fixed;
			left: 30rpx;
			top: 30rpx;
		}

		.dk-subtitle {
			font-size: 28rpx;
			color: #656366;
			line-height: 3;
			text-align: center;
		}
		
		.dk-down{
			text-align: center;
			line-height: 70rpx;
			box-shadow: 0 0 0 2rpx #eee;
			.icon{
				width: 40rpx;
				height: 40rpx;
			}
		}
		
		.pwd-tips{
			color: #666;
			font-size: 24rpx;
			line-height: 1.5;
			padding: 15rpx 100rpx;
		}
		
		.pwd-tips-errot{
			color: #ff4242;
			font-size: 30rpx;
			text-align: center;
		}
		
		.pwd-forget {
			font-size: 24rpx;
			color: #26C6B3;
			padding: 20rpx 0 30rpx;
			text-align: center;
			line-height: 33rpx;
		}

		.digital-keyboard{
			box-shadow: 0 -1rpx 0 3rpx #eee;
			width: 100%;
			transition: all .2s linear;
			transform: translateY(100%);
		}
		
		/* 键盘 */
		.digital-key-box {
			box-shadow: 0 0 0 2rpx #eee;
			position: relative;
			overflow: hidden;
			width: 100%;
		}
		
		.digital-keyboard-show{
			transform: translateY(0);
		}

		.num {
			transition: all .2s linear;
			float: left;
			background-color: #fff;
			box-shadow: 0 0 0 2rpx #eee;
			width: 33.33333333%;
			cursor: pointer;
			height: 110rpx;
			text-align: center;
			color: #333;
			line-height: 110rpx;
			font-size: 55rpx;
			font-weight: bold;
			position: relative;
			overflow: hidden;
			&:after {
				content: "";
				display: block;
				position: absolute;
				width: 100%;
				height: 100%;
				top: 0;
				left: 0;
				pointer-events: none;
				//设置径向渐变
				background-image: radial-gradient(circle, rgba(0, 0, 0, .65) 20%, transparent 20%);
				background-repeat: no-repeat;
				background-position: 50%;
				transform: scale(5);
				opacity: 0;
				transition: transform .2s ease-in-out, opacity .3s ease-in-out;
			}
			&:active:after {
			    transform: scale(0);
			    opacity: .5;
			    //设置初始状态
			    transition: 0s;
			}
		}

		.num:nth-of-type(3n+1) {
			border-bottom: 0;
			border-left: 0;
		}

		.num:nth-of-type(3n+2) {
			border-bottom: 0;
			border-left: 0;
			border-right: 0;
		}

		.num.no-num {
			background-color: #f2f2f2;
			font-size: 0;
		}
		

		.num:last-child {
			background: #f2f2f2 url() center center / auto 50rpx no-repeat;
		}

		.pwd-box {
			padding-left: 10rpx;
			position: relative;
			text-align: center;
		}

		.pwd-text {
			position: relative;
			line-height: 92rpx;
			vertical-align: middle;
			text-align: center;
			font-size: 50rpx;
			font-weight: bold;
			// width: 92rpx;
			// height: 92rpx;
			width: 74rpx;
			height: 74rpx;
			margin-right: 10rpx;
			display: inline-block;
			box-shadow: 0 0 0 2rpx #ccc;
			overflow: hidden;
		}

		.pwd-text.active:after {
			-webkit-animation: twinkle 1s infinite;
			animation: twinkle 1s infinite;
			height: 70%;
			width: 4rpx;
			content: '';
			position: absolute;
			top: 15%;
			left: 50%;
			margin-left: -2rpx;
			background-color: #4fa5e1;
		}
		
		@keyframes twinkle {
			from {
				background-color: #4fa5e1;
			}

			to {
				background-color: transparent;
			}
		}
	}
</style>
